<template>

  <el-dialog v-model="centerDialogVisible" title="增加project" width="40%" center>

    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right:30px;">
      <el-form-item label="name：" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="remark：" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="recive Emails：" prop="recEmails">
        <el-input v-model="form.recEmails" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="isRecEmail：" prop="isRecEmail">
        <el-switch v-model="form.isRecEmail"/>
      </el-form-item>
      <el-form-item label="sign：" prop="sign">
        <el-input v-model="form.sign" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="sign String：" prop="signString">
        <el-input v-model="form.signString" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submit">Submit</el-button>
        <el-button @click="handleClose">Cancel</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import {defineComponent} from 'vue'
import TaskProjectApi from "@/api/project/taskProjectApi";
import {ElMessage} from "element-plus";

export default defineComponent({
  name: 'AddProjectDialog',
  props: {
    centerDialogVisible: {
      type: Boolean,
      default: false
    },
    currentRow: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      form: {
        name: '',
        remark: '',
        recEmails: '',
        isRecEmail: false,
        sign: '',
        signString: ''
      },
      rules: {
        name: [{required: true, message: '请输入', trigger: 'blur'}],
        remark: [{required: true, message: '请输入', trigger: 'blur'}],
        recEmails: [{required: true, message: '请输入', trigger: 'blur'}],
        sign: [{required: true, message: '请输入', trigger: 'blur'}],
        signString: [{required: true, message: '请输入', trigger: 'blur'}],
      }
    }
  },
  watch: {
    currentRow(newVal) {
      if (newVal.id) {
        this.form = newVal
      } else {
        this.resetForm()
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    async submit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.form.taskGroups = []
          TaskProjectApi.add(this.form).then(() => {
            ElMessage({
              message: "添加成功",
              type: 'success',
              duration: 1000
            })
            this.handleClose()
          })
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.form = {
        name: '',
        remark: '',
        recEmails: '',
        isRecEmail: false,
        sign: '',
        signString: ''
      }
    }
  }
})
</script>

<style lang="scss" scoped>

</style>